package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Book;
import pojo.College;
import service.BackCollegeService;
import service.BackMallService;
import serviceImpl.BackCollegeServiceImpl;
import serviceImpl.BackMallServiceImpl;


@WebServlet("/BackMallServlet")
public class BackMallServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public BackMallServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String mark = request.getParameter("mark");
		System.out.println("mark:"+mark);
		switch(mark) {
		
		case "showBooks": 
			showBooks(request,response);
			break;
		case "addBook": 
			addBook(request,response);
			break;
		case "delBook": 
			delBook(request,response);
			break;
		
		}
	}
	
	private void delBook(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int bookid = Integer.parseInt(request.getParameter("bookid"));
		System.out.println("bookid:"+bookid);
		BackMallService bms = new BackMallServiceImpl();
		boolean bo = bms.delBook(bookid);
		System.out.println(bo);
		
		PrintWriter writer = response.getWriter();
		writer.print(bo);
		
	}

	private void addBook(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		int stid = Integer.parseInt(request.getParameter("stid"));
		String bookName = request.getParameter("bookName");
		int bookKind = Integer.parseInt(request.getParameter("bookKind"));
		Float bookPrice = Float.parseFloat(request.getParameter("bookPrice"));
		Float expressPrice = Float.parseFloat(request.getParameter("expressPrice"));
		String bookAuthor = request.getParameter("bookAuthor");
		String bookIntroduction = request.getParameter("bookIntroduction");
		String bookMainIma = request.getParameter("bookMainIma");
		System.out.println(stid);
		System.out.println(bookName);
		System.out.println(bookKind);
		System.out.println(bookPrice);
		System.out.println(expressPrice);
		System.out.println(bookAuthor);
		System.out.println(bookIntroduction);
		System.out.println(bookMainIma);
		BackMallService bms = new BackMallServiceImpl();
		boolean suc = bms.addBook(stid,bookName,bookKind,bookPrice,expressPrice,bookAuthor,bookIntroduction,bookMainIma);
		System.out.println("suc:"+suc);
		PrintWriter writer = response.getWriter();
		writer.print(suc);
		
	}

	private void showBooks(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		BackMallService bms = new BackMallServiceImpl();
		ArrayList<Book> bList = new ArrayList<Book>();
		bList = bms.getBooks();
		request.setAttribute("bookList", bList);
		request.getRequestDispatcher("/background/ordUserJs/index/book.jsp").forward(request, response);
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
